package com.yb0os1.etcd;

import io.etcd.jetcd.Client;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.StringRedisTemplate;


@Configuration
//我们这个configuration需要在RedisAutoConfiguration之前
@AutoConfigureBefore(RedisAutoConfiguration.class)
@EnableConfigurationProperties(EtcdProperties.class)
public class EctdConfig {

    @Bean
    public Client etcdClient(EtcdProperties etcdProperties) {
        return Client.builder().endpoints(etcdProperties.getEtcdHost()).build();
    }

    @Bean
    public EtcdValueOperations etcdValueOperations(Client etcdClient) {
        return new EtcdValueOperations(etcdClient);
    }

    @Bean("redisTemplate")
    public StringRedisTemplate redisTemplate(EtcdValueOperations etcdValueOperations) {
        return new EtcdRedisTemplateAdapter(etcdValueOperations);
    }

    @Bean
    public RedisKeyValueAdapter redisKeyValueAdapter() {
        return new VoidRedisKeyValueAdapter();
    }
}
